<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use Carbon\Carbon;

class ProductController extends Controller
{
    public function index(Request $request)
    {
    	$rs['status']     = false;
        $where['type_id'] = $request->type_id;
        $where['shop_id'] = $request->shop_id;
        $where['on_sale'] = 1;
        $num = object_get($request,'num',5);
        $fields = ['id','name','thumb','price','origin_price','weight','spec_id','weight'];
        $list = ProductRepo::getListPages($where,$num,$fields);
        if($list) {
            $rs['status'] = true;
            $rs['data'] = $list;
        }
        return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);;		
    }

    public function show(Request $request)
    {
        $rs['status']     = false;        
       
        $where['id'] = $request->product_id;
       
        $data      = ProductRepo::find($where);  
        $rs['data'] = $data;      
        return response()->json($rs);       
    }

    public function group(request $request)
    {
        $rs['status']     = false;        
        $group     = ProductRepo::group($request->product_id);
        $data = ProductRepo::userGroupbuy($group->id);
        if($data) {
            $rs['status'] = true;           
            $rs['data']['list']   = $data->sortByDesc('num');    
            $rs['data']['group_num'] = $group->group_num; 
            $rs['data']['total_num'] = $group->total_num; 

            
           
        }
        return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);   
    }
}
